/*
 * @(#)DrawingPanel.java
 *
 * Copyright (c) 1996-2010 by the original authors of JHotDraw and all its
 * contributors. All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the 
 * license agreement you entered into with the copyright holders. For details
 * see accompanying license terms.
 */
package org.jhotdraw.samples.draw;

import static org.jhotdraw.draw.AttributeKeys.END_DECORATION;

import java.util.Collection;

import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import org.jhotdraw.app.action.edit.CopyAction;
import org.jhotdraw.app.action.edit.CutAction;
import org.jhotdraw.app.action.edit.DuplicateAction;
import org.jhotdraw.app.action.edit.PasteAction;
import org.jhotdraw.app.action.edit.SelectAllAction;
import org.jhotdraw.draw.AbstractAttributedFigure;
import org.jhotdraw.draw.BezierFigure;
import org.jhotdraw.draw.ConnectionFigure;
import org.jhotdraw.draw.DefaultDrawing;
import org.jhotdraw.draw.DefaultDrawingEditor;
import org.jhotdraw.draw.DiamondFigure;
import org.jhotdraw.draw.Drawing;
import org.jhotdraw.draw.DrawingEditor;
import org.jhotdraw.draw.DrawingView;
import org.jhotdraw.draw.EllipseFigure;
import org.jhotdraw.draw.LineConnectionFigure;
import org.jhotdraw.draw.LineFigure;
import org.jhotdraw.draw.RectangleFigure;
import org.jhotdraw.draw.RoundRectangleFigure;
import org.jhotdraw.draw.TextAreaFigure;
import org.jhotdraw.draw.TextFigure;
import org.jhotdraw.draw.TriangleFigure;
import org.jhotdraw.draw.action.BringToFrontAction;
import org.jhotdraw.draw.action.ButtonFactory;
import org.jhotdraw.draw.action.GroupAction;
import org.jhotdraw.draw.action.SelectSameAction;
import org.jhotdraw.draw.action.SendToBackAction;
import org.jhotdraw.draw.action.UngroupAction;
import org.jhotdraw.draw.action.ZoomAction;
import org.jhotdraw.draw.decoration.ArrowTip;
import org.jhotdraw.draw.liner.CurvedLiner;
import org.jhotdraw.draw.liner.ElbowLiner;
import org.jhotdraw.draw.tool.BezierTool;
import org.jhotdraw.draw.tool.ConnectionTool;
import org.jhotdraw.draw.tool.CreationTool;
import org.jhotdraw.draw.tool.TextAreaCreationTool;
import org.jhotdraw.draw.tool.TextCreationTool;
import org.jhotdraw.gui.JPopupButton;
import org.jhotdraw.undo.UndoRedoManager;
import org.jhotdraw.util.ResourceBundleUtil;

/**
 * DrawingPanel.
 * 
 * 
 * @author Werner Randelshofer
 * @version $Id: DrawingPanel.java 717 2010-11-21 12:30:57Z rawcoder $
 */
public class DrawingPanel extends JPanel {

	private UndoRedoManager undoManager;
	private Drawing drawing;
	private DrawingEditor editor;

	/** Creates new instance. */
	public DrawingPanel() {
		ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels");
		initComponents();
		undoManager = new UndoRedoManager();
		editor = new DefaultDrawingEditor();
		editor.add(view);

		addCreationButtonsTo(creationToolbar, editor);
		ButtonFactory.addAttributesButtonsTo(attributesToolbar, editor);

		JPopupButton pb = new JPopupButton();
		pb.setItemFont(UIManager.getFont("MenuItem.font"));
		labels.configureToolBarButton(pb, "actions");
		pb.add(new DuplicateAction());
		pb.addSeparator();
		pb.add(new GroupAction(editor));
		pb.add(new UngroupAction(editor));
		pb.addSeparator();
		pb.add(new BringToFrontAction(editor));
		pb.add(new SendToBackAction(editor));
		pb.addSeparator();
		pb.add(new CutAction());
		pb.add(new CopyAction());
		pb.add(new PasteAction());
		pb.add(new SelectAllAction());
		pb.add(new SelectSameAction(editor));
		pb.addSeparator();
		pb.add(undoManager.getUndoAction());
		pb.add(undoManager.getRedoAction());
		// FIXME - We need a toggle grid action!
		// pb.addSeparator();
		// pb.add(new ToggleGridAction(editor));

		JMenu m = new JMenu(labels.getString("view.zoomFactor.text"));
		JRadioButtonMenuItem rbmi;
		ButtonGroup group = new ButtonGroup();
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 0.1, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 0.25, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 0.5, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 0.75, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 1.0, null)));
		rbmi.setSelected(true);
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 1.25, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 1.5, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 2, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 3, null)));
		group.add(rbmi);
		m.add(rbmi = new JRadioButtonMenuItem(new ZoomAction(editor, 4, null)));
		group.add(rbmi);
		pb.add(m);
		pb.setFocusable(false);
		creationToolbar.addSeparator();
		creationToolbar.add(pb);

		DefaultDrawing drawing = new DefaultDrawing();
		view.setDrawing(drawing);
		drawing.addUndoableEditListener(undoManager);
	}

	public void setDrawing(Drawing d) {
		undoManager.discardAllEdits();
		view.getDrawing().removeUndoableEditListener(undoManager);
		view.setDrawing(d);
		d.addUndoableEditListener(undoManager);
	}

	public Drawing getDrawing() {
		return view.getDrawing();
	}

	public DrawingView getView() {
		return view;
	}

	public DrawingEditor getEditor() {
		return editor;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		toolButtonGroup = new javax.swing.ButtonGroup();
		scrollPane = new javax.swing.JScrollPane();
		view = new org.jhotdraw.draw.DefaultDrawingView();
		jPanel1 = new javax.swing.JPanel();
		creationToolbar = new javax.swing.JToolBar();
		attributesToolbar = new javax.swing.JToolBar();

		setLayout(new java.awt.BorderLayout());

		scrollPane.setViewportView(view);

		add(scrollPane, java.awt.BorderLayout.CENTER);

		jPanel1.setLayout(new java.awt.GridBagLayout());

		creationToolbar.setFloatable(false);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		jPanel1.add(creationToolbar, gridBagConstraints);

		attributesToolbar.setFloatable(false);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		jPanel1.add(attributesToolbar, gridBagConstraints);

		add(jPanel1, java.awt.BorderLayout.SOUTH);

	}// </editor-fold>//GEN-END:initComponents

	private void addCreationButtonsTo(JToolBar tb, DrawingEditor editor) {
		addDefaultCreationButtonsTo(tb, editor, ButtonFactory.createDrawingActions(editor), ButtonFactory.createSelectionActions(editor));
	}

	public void addDefaultCreationButtonsTo(JToolBar tb, final DrawingEditor editor, Collection<Action> drawingActions,
			Collection<Action> selectionActions) {
		ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels");

		ButtonFactory.addSelectionToolTo(tb, editor, drawingActions, selectionActions);
		tb.addSeparator();

		AbstractAttributedFigure af;
		CreationTool ct;
		ConnectionTool cnt;
		ConnectionFigure lc;

		ButtonFactory.addToolTo(tb, editor, new CreationTool(new RectangleFigure()), "edit.createRectangle", labels);
		ButtonFactory.addToolTo(tb, editor, new CreationTool(new RoundRectangleFigure()), "edit.createRoundRectangle", labels);
		ButtonFactory.addToolTo(tb, editor, new CreationTool(new EllipseFigure()), "edit.createEllipse", labels);
		ButtonFactory.addToolTo(tb, editor, new CreationTool(new DiamondFigure()), "edit.createDiamond", labels);
		ButtonFactory.addToolTo(tb, editor, new CreationTool(new TriangleFigure()), "edit.createTriangle", labels);
		ButtonFactory.addToolTo(tb, editor, new CreationTool(new LineFigure()), "edit.createLine", labels);
		ButtonFactory.addToolTo(tb, editor, ct = new CreationTool(new LineFigure()), "edit.createArrow", labels);
		af = (AbstractAttributedFigure) ct.getPrototype();
		af.set(END_DECORATION, new ArrowTip(0.35, 12, 11.3));
		ButtonFactory.addToolTo(tb, editor, new ConnectionTool(new LineConnectionFigure()), "edit.createLineConnection", labels);
		ButtonFactory.addToolTo(tb, editor, cnt = new ConnectionTool(new LineConnectionFigure()), "edit.createElbowConnection", labels);
		lc = cnt.getPrototype();
		lc.setLiner(new ElbowLiner());
		ButtonFactory.addToolTo(tb, editor, cnt = new ConnectionTool(new LineConnectionFigure()), "edit.createCurvedConnection", labels);
		lc = cnt.getPrototype();
		lc.setLiner(new CurvedLiner());
		ButtonFactory.addToolTo(tb, editor, new BezierTool(new BezierFigure()), "edit.createScribble", labels);
		ButtonFactory.addToolTo(tb, editor, new BezierTool(new BezierFigure(true)), "edit.createPolygon", labels);
		ButtonFactory.addToolTo(tb, editor, new TextCreationTool(new TextFigure()), "edit.createText", labels);
		ButtonFactory.addToolTo(tb, editor, new TextAreaCreationTool(new TextAreaFigure()), "edit.createTextArea", labels);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JToolBar attributesToolbar;
	private javax.swing.JToolBar creationToolbar;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane scrollPane;
	private javax.swing.ButtonGroup toolButtonGroup;
	private org.jhotdraw.draw.DefaultDrawingView view;
	// End of variables declaration//GEN-END:variables
}
